package com.tdam2014.grupo02;

import java.io.IOException;
import java.util.ArrayList;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONException;

import com.tdam2014.grupo02.R;
import com.tdam2014.grupo02.FolderActivity;
import com.tdam2014.grupo02.R.drawable;

import android.app.ListFragment;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class PictureFragment extends ListFragment 
{

	private TextView txtPictureName;
	private ImageButton photo,share;
	
		
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
	{
		return inflater.inflate(R.layout.fragment_picture, null);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) 
	{ 	
		super.onActivityCreated(savedInstanceState);
		
		txtPictureName = (TextView) getView().findViewById(R.id.txtNombre);
		photo = (ImageButton) getView().findViewById(R.id.btn_picture);
		share = (ImageButton) getView().findViewById(R.id.btn_share);
		photo.setImageResource(drawable.ic_action_picture);
		if (getActivity().getIntent().getBooleanExtra(FolderActivity.KEY_LOAD_PICTURE_DATA, false)) 
			
		{
			setPictureName(getActivity().getIntent().getStringExtra(FolderActivity.KEY_PICTURE_NAME));
		}
		
		//Comment.generateComments(getPictureId(), getActivity());
		ArrayList<Comment> list = Comment.getComments(getActivity(), getPictureId());
		final CommentAdapter adapter = new CommentAdapter(getActivity());
		
		if (list != null)
		{
			adapter.setList(list);
		}
		else
		{
			adapter.setList(new ArrayList<Comment>());
		}
		
		setListAdapter(adapter);
		photo.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v) 
			{
				Picture pic = Picture.getPicture(getActivity(),String.valueOf(getPictureId()));
				
				String url = "https://www.flickr.com/photos/66222229@N03/";
				url+=pic.getPictureFlickrId();
				url+="/";
				Intent i = new Intent(Intent.ACTION_VIEW);
				i.setData(Uri.parse(url));
				startActivity(i);
			}
		});
		
		share.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v) 
			{
				Picture pic = Picture.getPicture(getActivity(),String.valueOf(getPictureId()));
				
				String url = "https://www.flickr.com/photos/66222229@N03/";
				url+=pic.getPictureFlickrId();
				url+="/";
				Intent intent = new Intent(Intent.ACTION_SEND);
				intent.setType("text/html");
				intent.putExtra(Intent.EXTRA_SUBJECT, "Mira mi foto en Flickr");
				intent.putExtra(Intent.EXTRA_TEXT, url);

				startActivity(Intent.createChooser(intent, "Send Email"));;
			}
		});
			
		

	}
	public void setPictureName(String name) {
		txtPictureName.setText(name);
	}
	public String getPictureName ()
	{
		return (String) txtPictureName.getText();
	}
	public int getPictureId()
	{
		return Integer.parseInt(getActivity().getIntent().getStringExtra(FolderActivity.KEY_PICTURE_ID));
	}
class JsonReader extends AsyncTask<Void, Void, Void> {
		
		public JsonReader() {}
			
		@Override
		protected Void doInBackground(Void... params) 
		{
			try {
				Comment.updatePicsComments(getActivity(), Picture.getPicture(getActivity(),String.valueOf(getPictureId())));
				//Picture.updateFolderPics(getActivity(), Folder.getFolder(getActivity(), String.valueOf(getFolderId())));
				
			} 
			catch (ClientProtocolException e) 
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		
			return null;
		}

		@Override
		protected void onPostExecute(Void result) 
		{
			ArrayList<Comment> list = Comment.getComments(getActivity(), getPictureId());
			final CommentAdapter adapter = new CommentAdapter(getActivity());
			
			if (list != null)
			{
				adapter.setList(list);
			}
			else
			{
				adapter.setList(new ArrayList<Comment>());
			}
			
			setListAdapter(adapter);
			

		
			
		}
	}
}
